package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.cart.*;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.CartMapper;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.RegionMapper;
import com.cskaoyan.weixin.bean.address.Address;
import com.cskaoyan.weixin.bean.address.AddressExample;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements  CartService{

    @Autowired
    CartMapper cartMapper;

    @Autowired
    RegionMapper regionMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CouponMapper couponMapper;

    @Override
    public CartIndexBean selectCartIndex() {
        Subject subject = SecurityUtils.getSubject();
        User1 user1 = (User1) subject.getPrincipal();
        Integer userid = user1.getId();

        CartIndexBean cartIndexBean = new CartIndexBean();

        CartTotalBean cartTotalBean = new CartTotalBean();
        Integer goodsCount = cartMapper.selectGoodsCount(userid);
        Integer checkedGoodsCount = cartMapper.selectCheckedGoodsCount(userid);
        Double goodsAmount = cartMapper.selectGoodsAmount(userid);
        Double checkedGoodsAmount = cartMapper.selectCheckGoodsAmount(userid);
        cartTotalBean.setGoodsCount(goodsCount);
        cartTotalBean.setCheckedGoodsCount(checkedGoodsCount);
        cartTotalBean.setGoodsAmount(goodsAmount);
        cartTotalBean.setCheckedGoodsAmount(checkedGoodsAmount);

        List<CartListBean> cartListBeans = cartMapper.selectListBeans(userid);


        cartIndexBean.setCartTotal(cartTotalBean);
        cartIndexBean.setCartList(cartListBeans);
        return cartIndexBean;
    }

    //xiugai -->  先查询没有再添加
    @Override
    public Integer addCart(CartListBean cartListBean) {
        Subject subject = SecurityUtils.getSubject();
        User1 user = (User1) subject.getPrincipal();
        Integer userid = user.getId();
//        specifications,
        //查询：根据goodsid 和userid 再cart表中查找
        Integer isHaved = cartMapper.selectCartIsHaved(cartListBean.getGoodsId(),userid);
        if(isHaved==0){   //cart没有加入过
            String[] specifications=cartMapper.selectSpecifications(cartListBean.getGoodsId());
            CartListBean cartListBean1 = cartMapper.selectMes(cartListBean.getGoodsId());
            cartListBean1.setUserId(userid);
            cartListBean1.setSpecifications(specifications);
            cartListBean1.setGoodsId(cartListBean.getGoodsId());
            cartListBean1.setProductId(cartListBean.getProductId());
            cartListBean1.setNumber(cartListBean.getNumber());

            Integer data = cartMapper.addCart(cartListBean1);
            return data;

        }else{
            cartMapper.updateCartNumber(cartListBean.getGoodsId(),userid,cartListBean.getNumber());
            return cartListBean.getNumber();
        }


//        cartMapper.addUpdateCart();

    }


    @Override
    public void updateCart(CartUpdateBean cartUpdateBean) {
        cartMapper.updateCart(cartUpdateBean);

    }

    @Override
    public void   delectCart(CartDelectBean cartDelectBean) {
        Subject subject = SecurityUtils.getSubject();
        User1 user1 = (User1) subject.getPrincipal();
        Integer userid = user1.getId();
        for (Integer productId : cartDelectBean.getProductIds()) {
            cartMapper.delectCart(productId,userid);
        }

    }

    @Override
    public Integer selectGoodsCount() {
        Subject subject = SecurityUtils.getSubject();
        User1 user = (User1) subject.getPrincipal();
        if (user == null) {
            return 0;
        }
        Integer userid = user.getId();
        Integer data = cartMapper.selectGoodsCount(userid);
        return data;
    }

    @Override
    public void updateChecked(CheckedBean checkedBean) {
        Subject subject = SecurityUtils.getSubject();
        User1 user = (User1) subject.getPrincipal();
        Integer userid = user.getId();
        Integer isChecked = checkedBean.getIsChecked();
        for (Integer productId : checkedBean.getProductIds()) {
            cartMapper.updateChecked(productId,isChecked,userid);
        }
    }
//    private int grouponPrice;             1
//    private int grouponRulesId;           1
//    private CheckedAddressBean checkedAddress;    1
//    private double actualPrice;                1
//    private double orderTotalPrice;      1
//    private double couponPrice;            1
//    private int availableCouponLength;        ?
//    private int couponId;                    1
//    private int freightPrice;                 1
//    private double goodsTotalPrice;         1
//    private int addressId;                   1
//    private List<CheckedGoodsListBean> checkedGoodsList;     1


//    Integer cartId;
//    Integer addressId;
//    Integer couponId;
//    Integer grouponRulesId;
//    @Override
//    public CartCheckoutBean checkoutCart(CheckoutBean checkoutBean) {
//        CartCheckoutBean cartCheckoutBean = new CartCheckoutBean();
////        List<Integer>  cartIds = cartMapper.selectCartId();
//
//        Subject subject = SecurityUtils.getSubject();
//        User1 user = (User1) subject.getPrincipal();
//        Integer userId = user.getId();
////        String province = regionMapper.selectNameById(address.getProvinceId());
////        String city = regionMapper.selectNameById(address.getCityId());
////        String area = regionMapper.selectNameById(address.getAreaId());
//        if(checkoutBean.getCartId()==0){      //购物车下单
//            CheckedAddressBean checkedAddressBean = new CheckedAddressBean();
//            if(checkoutBean.getAddressId() == 0){
//                cartCheckoutBean.setCheckedAddress(checkedAddressBean);
//            } else {
//                checkedAddressBean = cartMapper.selectAddress(checkoutBean.getAddressId());
//                cartCheckoutBean.setCheckedAddress(checkedAddressBean);
//                String province = regionMapper.selectNameById(checkedAddressBean.getProvinceId());
//                String city = regionMapper.selectNameById(checkedAddressBean.getCityId());
//                String area = regionMapper.selectNameById(checkedAddressBean.getAreaId());
//                String address = province+city+area+checkedAddressBean.getAddress();
//                checkedAddressBean.setAddress(address);
//            }
////        CheckedAddressBean checkedAddressBean = cartMapper.selectAddress(checkoutBean.getAddressId());
////        cartCheckoutBean.setCheckedAddress(checkedAddressBean);
////
//            //cha xun you wenti
//            List<CheckedGoodsListBean> checkedGoodsListBeans =cartMapper.selectCheckedGoodsList(userId);
//            cartCheckoutBean.setCheckedGoodsList(checkedGoodsListBeans);
//            Double goodsTotalPrice=0.0;
//            for (CheckedGoodsListBean checkedGoodsListBean : checkedGoodsListBeans) {
//                goodsTotalPrice=goodsTotalPrice+checkedGoodsListBean.getPrice()*checkedGoodsListBean.getNumber();
//            }
//            cartCheckoutBean.setGoodsTotalPrice(goodsTotalPrice);
//            Integer grouponRulesId = checkoutBean.getGrouponRulesId();
//            cartCheckoutBean.setGrouponRulesId(grouponRulesId);
//            Integer grouponPrice = 0;
////        if(grouponRulesId==0){
////                grouponPrice = 0;
////        }else{
////            grouponPrice = cartMapper.selectGrouponPrice(grouponRulesId);
////        }
//            cartCheckoutBean.setGrouponPrice(grouponPrice);
//            Integer couponId = checkoutBean.getCouponId();
//            cartCheckoutBean.setCouponId(couponId);
//
//            if (couponId == 0){
//                cartCheckoutBean.setAvailableCouponLength(0);
//            }else {
//                Integer availableCouponLength = cartMapper.selectCouponTotal(goodsTotalPrice, userId);
//                cartCheckoutBean.setAvailableCouponLength(availableCouponLength);
//            }
//
//            List<Integer> freightPrices = cartMapper.selectFreightPrice();
//            Integer freightMin = freightPrices.get(0);      //包邮价
//            Integer freightPrice = freightPrices.get(1);
//            if(goodsTotalPrice>=freightMin){
//                freightPrice=0;
//            }
//            cartCheckoutBean.setFreightPrice(freightPrice);
//            //xiugai--->
//            Double couponPrice = 0.0;
//            if (couponId != 0){
//                couponPrice = cartMapper.selectCouponPrice(couponId);
//            }
//            cartCheckoutBean.setCouponPrice(couponPrice);
//            //<----
//
//            Double actualPrice = goodsTotalPrice-couponPrice-grouponPrice+freightPrice;
//            Double orderTotalPrice = actualPrice;
//            cartCheckoutBean.setAddressId(checkoutBean.getAddressId());
//            cartCheckoutBean.setActualPrice(actualPrice);
//            cartCheckoutBean.setOrderTotalPrice(orderTotalPrice);
//
//            return cartCheckoutBean;
//
//        }else{                       //fastadd
//            List<CheckedGoodsListBean> checkedGoodsListBeans = cartMapper.selectByCartId(checkoutBean.getCartId());
//            cartCheckoutBean.setCheckedGoodsList(checkedGoodsListBeans);
//            CheckedAddressBean checkedAddressBean = new CheckedAddressBean();
//            if(checkoutBean.getAddressId() == 0){
//                cartCheckoutBean.setCheckedAddress(checkedAddressBean);
//            } else {
//                checkedAddressBean = cartMapper.selectAddress(checkoutBean.getAddressId());
//                cartCheckoutBean.setCheckedAddress(checkedAddressBean);
//            }
//            Double goodsTotalPrice=0.0;
//            for (CheckedGoodsListBean checkedGoodsListBean : checkedGoodsListBeans) {
//                goodsTotalPrice=goodsTotalPrice+checkedGoodsListBean.getPrice()*checkedGoodsListBean.getNumber();
//            }
//            cartCheckoutBean.setGoodsTotalPrice(goodsTotalPrice);
//            Integer grouponRulesId = checkoutBean.getGrouponRulesId();
//            cartCheckoutBean.setGrouponRulesId(grouponRulesId);
//            Integer grouponPrice = 0;
////        if(grouponRulesId==0){
////                grouponPrice = 0;
////        }else{
////            grouponPrice = cartMapper.selectGrouponPrice(grouponRulesId);
////        }
//            cartCheckoutBean.setGrouponPrice(grouponPrice);
//            Integer couponId = checkoutBean.getCouponId();
//            cartCheckoutBean.setCouponId(couponId);
//
//            if (couponId == 0){
//                cartCheckoutBean.setAvailableCouponLength(0);
//            }else {
//                Integer availableCouponLength = cartMapper.selectCouponTotal(goodsTotalPrice, userId);
//                cartCheckoutBean.setAvailableCouponLength(availableCouponLength);
//            }
//
//            List<Integer> freightPrices = cartMapper.selectFreightPrice();
//            Integer freightMin = freightPrices.get(0);
//            Integer freightPrice = freightPrices.get(1);
//            if(goodsTotalPrice>=freightMin){
//                freightPrice=0;
//            }
//            cartCheckoutBean.setFreightPrice(freightPrice);
//            //xiugai--->
//            Double couponPrice = 0.0;
//            if (couponId != 0){
//                couponPrice = cartMapper.selectCouponPrice(couponId);
//            }
//            cartCheckoutBean.setCouponPrice(couponPrice);
//            //<----
//
//            Double actualPrice = goodsTotalPrice-couponPrice-grouponPrice+freightPrice;
//            Double orderTotalPrice = actualPrice;
//            cartCheckoutBean.setAddressId(checkoutBean.getAddressId());
//            cartCheckoutBean.setActualPrice(actualPrice);
//            cartCheckoutBean.setOrderTotalPrice(orderTotalPrice);
//
//            return cartCheckoutBean;
//
//        }
//
////        CheckedAddressBean checkedAddressBean = new CheckedAddressBean();
////        if(checkoutBean.getAddressId() == 0){
////            cartCheckoutBean.setCheckedAddress(checkedAddressBean);
////        } else {
////            checkedAddressBean = cartMapper.selectAddress(checkoutBean.getAddressId());
////            cartCheckoutBean.setCheckedAddress(checkedAddressBean);
////        }
//////        CheckedAddressBean checkedAddressBean = cartMapper.selectAddress(checkoutBean.getAddressId());
//////        cartCheckoutBean.setCheckedAddress(checkedAddressBean);
//////
////        //cha xun you wenti
////        List<CheckedGoodsListBean> checkedGoodsListBeans =cartMapper.selectCheckedGoodsList(userId);
////        cartCheckoutBean.setCheckedGoodsList(checkedGoodsListBeans);
////        Double goodsTotalPrice=0.0;
////        for (CheckedGoodsListBean checkedGoodsListBean : checkedGoodsListBeans) {
////            goodsTotalPrice=goodsTotalPrice+checkedGoodsListBean.getPrice()*checkedGoodsListBean.getNumber();
////        }
////        Integer grouponRulesId = checkoutBean.getGrouponRulesId();
////        cartCheckoutBean.setGrouponRulesId(grouponRulesId);
////        Integer grouponPrice = 0;
//////        if(grouponRulesId==0){
//////                grouponPrice = 0;
//////        }else{
//////            grouponPrice = cartMapper.selectGrouponPrice(grouponRulesId);
////        }
//        cartCheckoutBean.setGrouponPrice(grouponPrice);
//        Integer couponId = checkoutBean.getCouponId();
//        cartCheckoutBean.setCouponId(couponId);
//
//        if (couponId == 0){
//            cartCheckoutBean.setAvailableCouponLength(0);
//        }else {
//            Integer availableCouponLength = cartMapper.selectCouponTotal(couponId);
//            cartCheckoutBean.setAvailableCouponLength(availableCouponLength);
//        }
//
//        List<Integer> freightPrices = cartMapper.selectFreightPrice();
//        Integer freightMin = freightPrices.get(0);
//        Integer freightPrice = freightPrices.get(1);
//        if(goodsTotalPrice>=freightMin){
//            freightPrice=0;
//        }
//        //xiugai--->
//        Double couponPrice = 0.0;
//        if (couponId != 0){
//            couponPrice = cartMapper.selectCouponPrice(couponId);
//        }
//        cartCheckoutBean.setCouponPrice(couponPrice);
//        //<----
//
//        Double actualPrice = goodsTotalPrice-couponPrice-grouponPrice+freightPrice;
//        Double orderTotalPrice = actualPrice;
//        cartCheckoutBean.setAddressId(checkoutBean.getAddressId());
//        cartCheckoutBean.setActualPrice(actualPrice);
//        cartCheckoutBean.setOrderTotalPrice(orderTotalPrice);
//
//        return cartCheckoutBean;
//    }

    @Override
    public Integer selectCartByFastAddBean(FastAddBean fastAddBean) {
        Subject subject = SecurityUtils.getSubject();
        User1 user = (User1) subject.getPrincipal();
        Integer userid = user.getId();
        //查询：根据goodsid 和userid 再cart表中查找 id
        Integer isHaved = cartMapper.selectCartIsHaved(fastAddBean.getGoodsId(),userid);
        if(isHaved==0) {   //cart中没有加入过 插入cart fanhui Id
            String[] specifications = cartMapper.selectSpecifications(fastAddBean.getGoodsId());
            CartListBean cartListBean1 = cartMapper.selectMes(fastAddBean.getGoodsId());
            cartListBean1.setUserId(userid);
            cartListBean1.setSpecifications(specifications);
            cartListBean1.setGoodsId(fastAddBean.getGoodsId());
            cartListBean1.setProductId(fastAddBean.getProductId());
            cartListBean1.setNumber(fastAddBean.getNumber());

            Integer data = cartMapper.addCart(cartListBean1);

        }
        Integer Id = cartMapper.selectCartIdIsHaved(fastAddBean.getGoodsId(),userid);
        return Id;
    }

    public CheckOutVo selectCheckOutVo(Integer userId, Integer cartId, Integer addressId, Integer couponId, Integer grouponRulesId) {
        CheckOutVo checkOutVo = new CheckOutVo();
        double totalPrice=0;
        double totalPrice1=0;
        GrouponRules grouponRules;
        List<Cart> carts =new ArrayList<>();
        if (grouponRulesId==0){
            checkOutVo.setGrouponRulesId(0);
            checkOutVo.setGrouponPrice((double)0);
        }
//        else {
//                grouponRules = grouponRulesMapper.selectByPrimaryKey(grouponRulesId);
//            checkOutVo.setGrouponRulesId(grouponRulesId);
//            checkOutVo.setGrouponPrice(grouponRules.getDiscount().doubleValue());
//        }
        if (cartId==0){
            CartExample cartExample = new CartExample();
            CartExample.Criteria criteria = cartExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andCheckedEqualTo(true);
            criteria.andDeletedEqualTo(false);
            carts = cartMapper.selectByExample(cartExample);
            for (Cart cart : carts) {
                totalPrice=cart.getPrice().doubleValue()*cart.getNumber()+totalPrice;
            }
            totalPrice1=totalPrice;
            checkOutVo.setCheckedGoodsList(carts);
            if (grouponRulesId==0){
                checkOutVo.setGoodsTotalPrice(totalPrice);
            }else {
                totalPrice=totalPrice-checkOutVo.getGrouponPrice();
                checkOutVo.setGoodsTotalPrice(totalPrice);
            }
        }else {
            Cart cart = cartMapper.selectByPrimaryKey(cartId);
            totalPrice=cart.getPrice().doubleValue()*cart.getNumber();
            totalPrice1=totalPrice;
            carts.add(cart);
            checkOutVo.setCheckedGoodsList(carts);
            if (grouponRulesId==0){
                checkOutVo.setGoodsTotalPrice(totalPrice);
            }else {
                totalPrice=totalPrice-checkOutVo.getGrouponPrice();
                checkOutVo.setGoodsTotalPrice(totalPrice);
            }
        }
        if (addressId==0){
            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andIsDefaultEqualTo(true);
            criteria.andDeletedEqualTo(false);
            List<Address> addresses = addressMapper.selectByExample(addressExample);
            if(addresses == null) {

            }
            checkOutVo.setAddressId(addresses.get(0).getId());
            checkOutVo.setCheckedAddress(addresses.get(0));
        }else {
            Address address = addressMapper.selectByPrimaryKey(addressId);
            checkOutVo.setAddressId(addressId);
            checkOutVo.setCheckedAddress(address);
        }
        if (totalPrice>88){
            checkOutVo.setFreightPrice((double)0);
            checkOutVo.setActualPrice(totalPrice);
            checkOutVo.setOrderTotalPrice(totalPrice);
        }else {
            checkOutVo.setFreightPrice((double)8);
            checkOutVo.setActualPrice(totalPrice+8);
            checkOutVo.setOrderTotalPrice(totalPrice+8);
        }
       /* if (couponId==0||couponId==-1){
            checkOutVo.setCouponId(couponId);
            checkOutVo.setCouponPrice((double)0);
            checkOutVo.setActualPrice(checkOutVo.getActualPrice());
            checkOutVo.setOrderTotalPrice(checkOutVo.getOrderTotalPrice());
        }else {
            Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
            checkOutVo.setCouponId(couponId);
            checkOutVo.setCouponPrice(coupon.getDiscount().doubleValue());
            checkOutVo.setActualPrice(checkOutVo.getActualPrice()-checkOutVo.getCouponPrice());
            checkOutVo.setOrderTotalPrice(checkOutVo.getOrderTotalPrice()-checkOutVo.getCouponPrice());
        }*/
        Integer availableCouponLength=cartMapper.selectCountCoupons(userId,totalPrice1);

        if (availableCouponLength==0){
            if (couponId==0){
                checkOutVo.setCouponId(0);
                checkOutVo.setCouponPrice((double)0);
                checkOutVo.setActualPrice(checkOutVo.getActualPrice());
                checkOutVo.setOrderTotalPrice(checkOutVo.getOrderTotalPrice());
                checkOutVo.setAvailableCouponLength(0);
            }else if (couponId==-1){
                checkOutVo.setCouponId(-1);
                checkOutVo.setCouponPrice((double)0);
                checkOutVo.setActualPrice(checkOutVo.getActualPrice());
                checkOutVo.setOrderTotalPrice(checkOutVo.getOrderTotalPrice());
                checkOutVo.setAvailableCouponLength(0);
            }
        }else {
            if (couponId==0){
                Coupon coupon=cartMapper.selectMaxDiscount(userId,totalPrice1);
                checkOutVo.setCouponId(coupon.getId());
                checkOutVo.setCouponPrice(coupon.getDiscount().doubleValue());
                checkOutVo.setActualPrice(checkOutVo.getActualPrice()-checkOutVo.getGrouponPrice());
                checkOutVo.setOrderTotalPrice(checkOutVo.getOrderTotalPrice()-checkOutVo.getGrouponPrice());
                checkOutVo.setAvailableCouponLength(availableCouponLength);
            }else if (couponId==-1){
                checkOutVo.setCouponId(-1);
                checkOutVo.setCouponPrice((double)0);
                checkOutVo.setActualPrice(checkOutVo.getActualPrice());
                checkOutVo.setOrderTotalPrice(checkOutVo.getOrderTotalPrice());
                checkOutVo.setAvailableCouponLength(availableCouponLength);
            }else {
                Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
                checkOutVo.setCouponId(couponId);
                checkOutVo.setCouponPrice(coupon.getDiscount().doubleValue());
                checkOutVo.setActualPrice(checkOutVo.getActualPrice()-checkOutVo.getCouponPrice());
                checkOutVo.setOrderTotalPrice(checkOutVo.getOrderTotalPrice()-checkOutVo.getCouponPrice());
                checkOutVo.setAvailableCouponLength(availableCouponLength);
            }
        }
        return checkOutVo;
    }


}
